package com.ray.auth.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ray.auth.mapper.UserMapper;
import com.ray.auth.model.User;
import com.ray.auth.service.AuthService;
import com.ray.auth.util.UserEncodes;
import com.ray.common.constants.Constants;
import com.ray.framework.base.Page;
import com.ray.framework.key.GeneratePrimaryKey;
import com.ray.util.DateUtil;
import com.ray.util.fmt.FormatFactory;

@Service("authService")
public class AuthServiceImpl implements AuthService {

    private static final Logger log = LoggerFactory.getLogger(AuthServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    public Page<User> paginateUser(Page<User> page) {
        List<User> users = userMapper.paginateUserPage(page);
        if (log.isDebugEnabled()) {
            log.debug("{}", FormatFactory.objectToJson(users));
        }
        page.setResultList(users);
        return page;
    }

    @Override
    public boolean insertUser(User user) {
        user.setId(GeneratePrimaryKey.getPkValue("T_AUTH_USER"));
        user.setIsDel(Constants.IS_DEL_N);
        user.setCreateTime(DateUtil.getSystemDateTime());
        UserEncodes.entryptPassword(user);
        int flag = userMapper.insert(user);
        if (flag != 1) {
            return false;
        }
        return true;
    }

    @Override
    public void updateUser(User user) {
        int flag = userMapper.update(user);
        log.warn("## 修改用户返回结果={}", flag);
    }

    @Override
    public User getUserByUserId(String userId) {
        return userMapper.findById(userId);
    }

}
